package Soso.entity;

import Soso.service.CallService;
import Soso.service.SendService;
import Soso.share.Common;

public class TalkPackage extends SerPackage implements SendService, CallService {
    private int talkTime;// 通话时长(分钟)
    private int smsCount;// 短信条数（条）

    public TalkPackage() {
        //初始化信息
        this.talkTime = 500;
        this.smsCount = 30;
        this.price = 58.0;
    }

    public TalkPackage(int talkTime, int smsCount) {
        this.talkTime = talkTime;
        this.smsCount = smsCount;
    }

    /**
     * 获取
     * @return talkTime
     */
    public int getTalkTime() {
        return talkTime;
    }

    /**
     * 设置
     * @param talkTime
     */
    public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
    }

    /**
     * 获取
     * @return smsCount
     */
    public int getSmsCount() {
        return smsCount;
    }

    /**
     * 设置
     * @param smsCount
     */
    public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
    }

    /**
     * 显示套餐
     */
    @Override
    public void showInfo() {
        System.out.println("话唠套餐：通话时长为" + this.talkTime + "分钟/月，短信条数为"
                + this.smsCount + "条/月，资费为" + this.price + "元/月。");
    }

    /**
     * 判断剩余余额是否满足使用
     * @param minCount
     * @param card
     * @return
     * @throws Exception
     */
    public int call(int minCount,ModelCard card) throws Exception{
        int temp = minCount;
        for (int i = 0; i < minCount; i++) {
            if (this.talkTime-card.getRealTalkTime()>=1){
                //第一种情况：套餐剩余通话时长可以负1分钟通话
                card.setRealTalkTime(card.getRealTalkTime()+1);//实际使用流量加1MB
            } else if (card.getMoney()>=0.2) {
                //第二种情况：套餐通话时长已用完，账户余额可以支付1分钟通话，使用账户余额支付
                card.setRealTalkTime(card.getRealTalkTime()+1);//实际使用通话时长1分钟
                card.setMoney(Common.sub(card.getMoney(),0.2));//账户余额消费0.2元
                card.setConsumAmount(card.getConsumAmount()+0.2);
            }else {
                temp = i;
                throw new Exception("本次已通话"+temp+"分钟，您的余额不足，请充值后在使用！");
            }
        }
        return temp;
    }

    /**
     * 判断剩余余额是否还能发送短信
     * @param smsCount
     * @param card
     * @return
     * @throws Exception
     */
    public int sendMessage(int smsCount,ModelCard card) throws Exception{
        int temp = smsCount;
        for (int i = 0; i < smsCount; i++) {
            if (this.smsCount - card.getRealISMSCount()>=1){
                //第一种情况：套餐剩余短信条数可以付1条短信
                card.setRealISMSCount(card.getRealISMSCount()+1);//实际使用短信条数+1
                card.setMoney(Common.sub(card.getMoney(),0.1));
                card.setConsumAmount(card.getConsumAmount()+0.1);
            }else {
                temp = i;
                throw new Exception("本次已发送短信"+temp+"条，您的余额不足，请充值后在使用！");
            }
        }
        return temp;
    }
}
